// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_comparison, deprecated_member_use

import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base_new/help/JJEmpty.dart';

/// 一个队列 存了很多任务，在需要的时候选择指定的任务执行。
class FFDelayTaskManager {
  // 单例公开访问点
  factory FFDelayTaskManager() => _sharedInstance();

  // 静态、同步、私有访问点
  static FFDelayTaskManager _sharedInstance() {
    return _instance;
  }

  // 静态私有成员，没有初始化
  static final FFDelayTaskManager _instance = FFDelayTaskManager._();

  // 私有构造函数
  FFDelayTaskManager._() {}

  List<FFDelayTask> taskList = [];

  /// 添加任务
  bool addTask(
    FFDelayTask task, {
    bool run = false, // 立即执行
  }) {
    if (strIsEmpty(task.name)) return false;

    if (taskList.any((element) => element.name == task.name)) {
      return false;
    }

    taskList.add(task);
    if (run) {
      startTask(task.name);
    }
    return true;
  }

  /// 启动任务
  Future<dynamic> startTask(String name) async {
    if (strIsEmpty(name)) return;

    FFDelayTask? task;
    for (FFDelayTask t in taskList) {
      if (t.name == name) {
        task = t;
        break;
      }
    }
    if (task == null) return;

    if (task.state == FFDelayTaskState.ready) {
      task.state = FFDelayTaskState.running;
      try {
        var result = await task.start(task.params);
        task.state = FFDelayTaskState.complete;
        return result;
      } catch (e) {
        log_error("FFDelayTaskManager 任务 ${name}: 执行时出错 ${e.toString()}");
        task.state = FFDelayTaskState.complete;
        removeTask(name);
      }

      return null;
    }

    return null;
  }

  /// 移除任务
  removeTask(String name) {
    taskList.removeWhere((element) {
      bool result = element.name == name;
      if (result) {
        element.state = FFDelayTaskState.complete;
      }
      return result;
    });
  }

  /// 移除所有任务
  removeAllTask() {
    taskList.removeWhere((element) {
      element.state = FFDelayTaskState.complete;
      return true;
    });
  }

  /// 查询任务
  List<String> queryTask() {
    List<String> result = [];
    for (FFDelayTask task in taskList) {
      result.add(task.name);
    }
    return result;
  }
}

/// 抽象任务
abstract class FFDelayTask {
  /// 任务名
  late String name;

  /// 任务状态
  FFDelayTaskState state = FFDelayTaskState.ready;

  /// 任务参数
  late Map? params;

  /// 执行任务
  late Future<dynamic> Function(Map? params) start;

  /// 销毁任务对象
  void dispose() {
    FFDelayTaskManager().removeTask(name);
  }
}

/// 任务状态
enum FFDelayTaskState {
  /// 默认
  ready,

  /// 运行中
  running,

  /// 完成
  complete,
}

/// 例子
class MyTask extends FFDelayTask {
  MyTask() {
    name = "MyTask";
    params = {"name": "MyTask"};
    start = (params) async {
      print("MyTask start");

      // 执行完后，记得调用dispose()销毁对象
    };
  }
}
